<?php

namespace App\Http\Controllers\Lottery;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    //获取图片
    public function getPicture(Request $request){
        $type = $request->get('type');
        $data = DB::table('picture')->where('type','=',$type)->orderby('id','desc')->limit(5)->get();
        if(!$data){
            return response()->json(['msg'=>'0','data'=>'获取失败']);
        }
        return response()->json(['msg'=>'1','data'=>$data]);
    }
    //获取往期数据
    public function getLotteryNum(Request $request){
        $type =  $request->get('type');
        $limit = $request->get('limit')&&is_numeric($request->get('limit')) ?  $request->get('limit')  : 20;
        $data2 = '该游戏没有该功能';
        $data3 = array();
        switch($type) {
            case '1':
                $data = $this->dogetLotteryNum('黑龙江11选5',$limit,11,5);
                $data2 = $this->setGuanZhuHaoMa('黑龙江11选5');
                break;
            case '2':
                $n = 0;
                $m = 0;
                $data = $this->dogetLotteryNum('黑龙江快乐十分',$limit,20,8);
                $data3['lianhaoyilou'] = array();
                $data3['jiouyilou'] = array();
                //前三尾分布和遗漏
                $ree = $this->getFirstThreeWeiMissing($data['lianhao']);
                $data3['qswfbu'] = $ree['qiansanweifenbu'];
                unset($ree['qiansanweifenbu']);
                $data3['qswfbylou'] = $ree;
                $count = count($data['lianhao'])-1;
                for($i=$count;$i>=0;$i--){
                    $n++;
                    $m++;
                    $keys1 = array_filter(array_keys($data['lianhao'][$i]), function($key) {
                        return ($key & 1);
                    });
                    $keys2 = array_filter(array_keys($data['lianhao'][$i]), function($key) {
                        return (!($key & 1));
                    });
                    $array_odd = array_intersect_key($data['lianhao'][$i], $keys1);
                    $array_even = array_intersect_key($data['lianhao'][$i], $keys2);
                    sort($array_odd);
                    sort($array_even);
                    $nn = count($array_odd);
                    $mm = count($array_even);
                    $nnn=-1;$mmm=-1;
                    if($mm > 3){
                        $array_even[0]+6==$array_even[3]?
                            $mm>4&&$array_even[0]+8==$array_even[4]?
                                $mm>5&&$array_even[0]+10==$array_even[5]?
                                    $mm>6&&$array_even[0]+12==$array_even[6]?
                                        $mm>7&&$array_even[0]+14==$array_even[7]?
                                            $mmm=8 and $m = 0
                                            :$mmm=7 and $m = 0
                                        :$mmm=6 and $m = 0
                                    :$mmm=5 and $m = 0
                                :$mmm=4 and $m = 0
                            :$mmm=0;
                    }
                    if($nn > 3){
                        $array_odd[0]+6==$array_odd[3]?
                            $nn>4&&$array_odd[0]+8==$array_odd[4]?
                                $nn>5&&$array_odd[0]+10==$array_odd[5]?
                                    $nn>6&&$array_odd[0]+12==$array_odd[6]?
                                        $nn>7&&$array_odd[0]+14==$array_odd[7]?
                                            $nnn=8 and $m = 0
                                            :$nnn=7 and $m = 0
                                        :$nnn=6 and $m = 0
                                    :$nnn=5 and $m = 0
                                :$nnn=4 and $m = 0
                            :$nnn=0;
                    }
                    $nnn>$mmm?$nm=$nnn:$nm=$mmm;
                    $nm==0?array_push($data3['jiouyilou'],$m):array_push($data3['jiouyilou'],-$nm);
                    $re = $this->getContinuityNum($data['lianhao'][$i],20,8);
                    if($re && count($re[0])>3){
                        array_push($data3['lianhaoyilou'],-count($re[0]));
                        $n = 0;
                    }else{
                        array_push($data3['lianhaoyilou'],$n);
                    }
                }
                $data2 = $this->setGuanZhuHaoMa('黑龙江快乐十分');
                break;
            case '3':
                $data = $this->dogetLotteryNum('吉林快3',$limit,6,3);
                break;
            case '4':
                $table = '吉林时时彩';
                $data = $this->dogetLotteryNum($table,$limit,9,5);
                unset($data['lianhao']);
                foreach ($data as $key =>$value){
                    $re =  $this->jiLinShiShiCai($data[$key]);
                    $data[$key]['qiansanheweidanshuang'] =$re[0];//前三和值尾单双
                    $data[$key]['zhongsanheweidanshuang'] =$re[1];//中三和值尾单双
                    $data[$key]['housanheweidanshuang'] =$re[2];//后三和值尾单双
                    $data[$key]['qiansanheweidaxiao'] =$re[3];//前三和值尾大小
                    $data[$key]['zhongsanheweidaoxiao'] =$re[4];//中三和值尾大小
                    $data[$key]['housanheweidaoxiao'] =$re[5];//后三和值尾大小
                    $data[$key]['housankuadu'] =$re[6];//后三跨度
                    $data[$key]['qiansanhewei'] =$re[7];//前三和尾
                    $data[$key]['zhongsanhewei'] =$re[8];//中三和尾
                    $data[$key]['housanhewei'] =$re[9];//后三和尾
                }
                break;
            case '5':
                $data = $this->dogetLotteryNum('四川金7乐',$limit,7,3);
                $re = array();
                $count = count($data['lianhao']);
                for($i=0;$i<$count;$i++){
                    array_pop($data['lianhao'][$i]);
                }
                for($k=1;$k<8;$k++){
                    $m = 1;
                    for($j=0;$j<$count;$j++){
                        if(in_array($k,$data['lianhao'][$j])){
                            $re[$j][$k] = 0;
                            $m = 0;
                        }else{
                            $re[$j][$k] = $m;
                        }
                        $m++;
                    }
                }
                $data3['qianquyilou'] = $re;
                break;
            case '6':
                $data = $this->dogetLotteryNum('四川快乐12',$limit,12,5);
                $data2 = $this->setGuanZhuHaoMa('四川快乐12');
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }
        unset($data['lianhao']);
        return response()->json(['msg'=>'1','data'=>$data,'tuijianstate'=>$data2,'yilou'=>$data3]);
    }

    public function dogetLotteryNum($type,$limit,$max,$size){
        $data = DB::table($type)->select('ID','期号','开奖号','关注号')->orderby('期号','desc')->limit($limit)->get();
        $data = obj2arr($data);
        $result = array();
        $winArray = [];
        foreach($data as $v){
            $v['guanzhu'] = explode('-',$v['关注号']);
            $type == '吉林快3'||$type == '吉林时时彩' ? $v['开奖号'] = str_split($v['开奖号']): $v['开奖号'] = explode('-',$v['开奖号']);
            if($type === '四川金7乐' || $type == '吉林快3' || $type == '吉林时时彩'){
                foreach($v['开奖号'] as $k => $vv){
                    if(strlen($vv) === 1){
                        $v['开奖号'][$k]  = '0'.$vv;
                    }
                }
            }
            array_push($result,$v);
        }

        foreach ($result as $key =>$value){
            $con= $this->getContinuityNum($v['开奖号'],$max,$size);
            $result[$key]['lianhao'] = $con?$con[0]:[];
            $result[$key]['danhaoyilou'] = $this->getMissing($type,$result[$key]);//单号遗漏
            if($type === '黑龙江快乐十分'){
                //前三分布的遗漏
                $result[$key]['qiansanyilou'] = $this->getFirstThreeMissing($result[$key]['开奖号'],$result[$key]['期号']);
                //尾数分布和尾数遗漏
                $weishu= $this->getMantissa($result[$key]['开奖号'],$result[$key]['期号']);
                $result[$key]['weishufenbu'] = $weishu['weifenbu'];
                $result[$key]['weishuyilou'] = $weishu['weiyilou'];
            }
            //开奖号码数组
            array_push($winArray,$result[$key]['开奖号']);
            $repeat= $this->getRepeatNum($result[$key],$type);
            $span= $this->span($result[$key]['开奖号'],$type);
            $result[$key]['qiansanhewei'] =$this->firstSumLast($result[$key]['开奖号']);//前三和尾
            $result[$key]['chonghao'] = $repeat[0];//重号
            $result[$key]['qiansanchonghao'] = $repeat[1];//前3重号
            $result[$key]['kuadu']= $span[0];//跨度
            $result[$key]['qiankua']= $span[1];//前跨
            $result[$key]['hezhi']= $span[3];//和值
            $result[$key]['qiansanhezhi']= $span[2];//前三和值
            $result[$key]['jishu']= $span[4];//奇数
            $result[$key]['dashu']= $span[5];//大数
            $result[$key]['hezhidaxiao']= $span[6];//和值大小
            $result[$key]['hezhijiou']= $span[7];//和值单双
            $result[$key]['qihao']=  $result[$key]['期号'];//期号
            $result[$key]['kaijianghao']=  $result[$key]['开奖号'];//开奖号
        }
            $result['lianhao'] = $winArray;
        return $result;
    }

    //号码推荐更新

    public function setGuanZhuHaoMa($type){
        $state = DB::table('admin_game')->select('号码推荐状态')->where('游戏中文名',$type)->first();
        $state = $state->号码推荐状态;
        if(!$state){
            $data = DB::table($type)->select('期号','开奖号','关注号')->orderby('ID','desc')->first();
            $data = obj2arr($data);
            $re = $this->updateRecommend($type,$data['开奖号']);
            if($re){
                DB::table('admin_game')->where('游戏中文名',$type)->update(['号码推荐状态'=>'1']);
                return '更新成功';
            }else{
                return '更新失败';
            }
        }else{
            return '已更新';
        }
    }
    /*
     * 更新推荐号码中奖信息
     */
    public function  updateRecommend($table,$string){
        $sjn = 0;
        if($table == '黑龙江11选5'){
            $sjn = 11;
        }
        if($table == '黑龙江快乐十分'){
            $sjn = 20;
        }
        if($table == '四川快乐12'){
            $sjn = 12;
        }
        if(!$sjn){
            return '该游戏没有该功能';
        }
        $x = array();
        $y = array();
        $z = array();
        $c = array();
        $v = array();
        $array =   explode('-',$string);
        sort($array);
        //dd($array);
        $r = DB::table('admin_game')->where('游戏中文名',$table)->select('号码推荐')->get();
        preg_match_all("/\[.*?\//",$r[0]->号码推荐,$a);//推荐号
        $a[0][0]=trim($a[0][0],'[/');
        $a[0][1]=trim($a[0][1],'[/');
        $a[0][2]=trim($a[0][2],'[/');
        $a[0][3]=trim($a[0][3],'[/');
        $a[0][4]=trim($a[0][4],'[/');
        preg_match_all("/推出.*?期/",$r[0]->号码推荐,$b);//推出期数
        $b[0][0]=trim($b[0][0],'推出期');
        $b[0][1]=trim($b[0][1],'推出期');
        $b[0][2]=trim($b[0][2],'推出期');
        $b[0][3]=trim($b[0][3],'推出期');
        $b[0][4]=trim($b[0][4],'推出期');
        preg_match_all("/任选.*?\]/",$r[0]->号码推荐,$c);//任选234
        $string1 = $c[0][0];
        $string2 = $c[0][1];
        $string3 = $c[0][2];
        $string4 = $c[0][3];
        $string5 = $c[0][4];
        //任选二
        //中奖的2位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                array_push($x,$array[$i].' '.$array[$j]);
            }
        }
        //dd($x);
        //预测的号码
        $numString = explode(',',$a[0][0]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $x)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        //随机出2号码
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-1);
                $w = rand($q+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }

                if($w<10){
                    $w = '0'.$w;
                }
                $new1 .= $q.' '.$w.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string1='任选二['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][0].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][0]+1;
            $aa = '推出'.$aa.'期';
            $string1 = preg_replace("/推出.*?期/",$aa,$string1);
        }
        //任选三1
        //中奖的3位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    array_push($y,$array[$i].' '.$array[$j].' '.$array[$s]);
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][1]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $y)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        //随机出号码
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-2);
                $w = rand($q+1,$sjn-1);
                $e = rand($w+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }
                if($w<10){
                    $w = '0'.$w;
                }
                if($e<10){
                    $e = '0'.$e;
                }
                $new1 .= $q.' '.$w.' '.$e.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string2='任选三['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][1].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][1]+1;
            $aa = '推出'.$aa.'期';
            $string2 = preg_replace("/推出.*?期/",$aa,$string2);
        }
        //任选三2
        //中奖的3位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    array_push($z,$array[$i].' '.$array[$j].' '.$array[$s]);
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][2]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $z)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        //随机出号码
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-2);
                $w = rand($q+1,$sjn-1);
                $e = rand($w+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }
                if($w<10){
                    $w = '0'.$w;
                }
                if($e<10){
                    $e = '0'.$e;
                }
                $new1 .= $q.' '.$w.' '.$e.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string3='任选三['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][2].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][2]+1;
            $aa = '推出'.$aa.'期';
            $string3 = preg_replace("/推出.*?期/",$aa,$string3);
        }
        //任选四
        //中奖的4位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    for($k=$s+1;$k<count($array);$k++){
                        array_push($c,$array[$i].' '.$array[$j].' '.$array[$s].' '.$array[$k]);
                    }
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][3]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $c)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-3);
                $w = rand($q+1,$sjn-2);
                $e = rand($w+1,$sjn-1);
                $r = rand($e+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }

                if($w<10){
                    $w = '0'.$w;
                }

                if($e<10){
                    $e = '0'.$e;
                }
                if($r<10){
                    $r = '0'.$r;
                }
                $new1 .= $q.' '.$w.' '.$e.' '.$r.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string4='任选四['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][3].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][3]+1;
            $aa = '推出'.$aa.'期';
            $string4 = preg_replace("/推出.*?期/",$aa,$string4);
        }
        //任选四2
        //中奖的4位组合
        for($i=0;$i<count($array);$i++){
            for($j=$i+1;$j<count($array);$j++){
                for($s=$j+1;$s<count($array);$s++){
                    for($k=$s+1;$k<count($array);$k++){
                        array_push($v,$array[$i].' '.$array[$j].' '.$array[$s].' '.$array[$k]);
                    }
                }
            }
        }
        //预测的号码
        $numString = explode(',',$a[0][4]);
        $bool = false;
        $new1 = '';
        for($s=0;$s<count($numString);$s++) {
            if (in_array($numString[$s], $v)) {
                $bool = true;
                $numString[$s] = $numString[$s].'√';
            }
        }
        if($bool == 1){
            for($d=0;$d<3;$d++){
                $q = rand(1,$sjn-3);
                $w = rand($q+1,$sjn-2);
                $e = rand($w+1,$sjn-1);
                $r = rand($e+1,$sjn);
                if($q<10){
                    $q = '0'.$q;
                }

                if($w<10){
                    $w = '0'.$w;
                }

                if($e<10){
                    $e = '0'.$e;
                }
                if($r<10){
                    $r = '0'.$r;
                }
                $new1 .= $q.' '.$w.' '.$e.' '.$r.',';

            }
            $new1 = rtrim($new1,',');
            $q = $numString[0].','.$numString[1].','.$numString[2];
            $string5='任选四['.$new1.'/推出1期/上次推荐'.$q.'/'.$b[0][4].'期中出]';
        }
        if($bool != 1){
            $aa = $b[0][4]+1;
            $aa = '推出'.$aa.'期';
            $string5 = preg_replace("/推出.*?期/",$aa,$string5);
        }
        $upString = $string1.'   '.$string2.'   '.$string3.'   '.$string4.'   '.$string5;
        $re = DB::table('admin_game')->where('游戏中文名',$table)->update(['号码推荐'=>$upString]);
        if($re == 1){
            return true;
        }else{
            return false;
        }
    }
    //获取开奖时间

    public function getLotteryTime(Request $request){
        $type =  $request->get('type');
        switch($type) {
            case '1':
                $data = $this->dogetLotteryTime('黑龙江11选5');
                break;
            case '2':
                $data = $this->dogetLotteryTime('黑龙江快乐十分');
                break;
            case '3':
                $data = $this->dogetLotteryTime('吉林快3');
                break;
            case '4':
                $data = $this->dogetLotteryTime('吉林时时彩');
                break;
            case '5':
                $data = $this->dogetLotteryTime('四川金7乐');
                break;
            case '6':
                $data = $this->dogetLotteryTime('四川快乐12');
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }
        if(isset($data['tag'])){


            return response()->json(['msg'=>'2','data'=>$data]);
        }
        return response()->json(['msg'=>'1','data'=>$data]);

    }

    public  function  dogetLotteryTime($type){
        $result = obj2arr(DB::table('admin_game')->select('起售时间','全天期数','每期间隔秒数','与北京时间误差秒数','关注号码')->where('游戏中文名',$type)->first());
        $b_time = strtotime(date('Y-m-d',time()).' '.$result['起售时间']) + $result['与北京时间误差秒数'];
        $next_stage = ceil((time()-$b_time)/$result['每期间隔秒数']);
        $n_time = $b_time + $next_stage*$result['每期间隔秒数']; //下期开奖时间
        $data['guanzhu'] =explode('-',$result['关注号码']);
        $data['next_stage'] = $next_stage;
        $data['ts'] = $result['全天期数'];
        if($next_stage > $result['全天期数'] || $next_stage < 1){
            $data['tag'] = 0;
            return $data;
        }
        $data['n_time'] = $n_time - time();
        return $data;
    }
    /*
     * 获取每期开奖号码的单号遗漏
     */
    public  function getMissing($table,$newWin){


        $bool = true;
        $zeroStart = false;
        switch ($table){
            case  '黑龙江11选5':
                $count = 12;
                $totalStage = 88;
                break;
            case  '黑龙江快乐十分':
                $count = 21;
                $totalStage = 84;
                break;
            case  '吉林快3':
                $count = 7;
                $bool = false;
                $totalStage = 87;
                break;
            case  '吉林时时彩':
                $count = 10;
                $bool = false;
                $zeroStart = true;
                $totalStage = 87;
                break;
            case  '四川金7乐':
                $count = 13;
                $totalStage = 84;
                break;
            case  '四川快乐12':
                $count = 13;
                $totalStage = 78;
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }



        $rightNum =$newWin['开奖号'];
        if($zeroStart === true){
            for($i = 0;$i<$count;$i++){
                if(in_array($i,$rightNum)){
                    $r[$i]=0;
                }else{
                    $s = $i;
                    if($i<10){

                        $s = '0'.$i;
                        if($bool == false){
                            $s = $i;
                        }
                    }
                    $num = DB::table($table)->where('开奖号','like','%'.$s.'%')->where('期号','<',$newWin['期号'])->select('期号')->orderby('期号','desc')->first();
                    $Date_1 = $newWin['期号'];
                    $Date_2 = $num->期号;

                    $d1 = strtotime($Date_1);
                    $d2 = strtotime($Date_2);
                    $days = (int)round(($d2-$d1)/3600/24);
                    $realStage = substr($newWin['期号'],-3);
                    $r[$i] = ($days*$totalStage)+$realStage - substr($num->期号,-3);
                    if($days===0){
                        $r[$i] = $newWin['期号']-$num->期号;
                    }
                }
            }
        }else{
            for($i = 1;$i<$count;$i++){
                if(in_array($i,$rightNum)){
                    $r[$i]=0;
                }else{
                    $s = $i;
                    if($i<10){

                        $s = '0'.$i;
                        if($bool == false){
                            $s = $i;
                        }
                    }
                    $num = DB::table($table)->where('开奖号','like','%'.$s.'%')->where('期号','<',$newWin['期号'])->select('期号')->orderby('期号','desc')->first();
                    $Date_1 = substr($newWin['期号'],0,2).'-'.substr($newWin['期号'],2,2).'-'.substr($newWin['期号'],4,2);
                    $Date_2 = substr($num->期号,0,2).'-'.substr($num->期号,2,2).'-'.substr($num->期号,4,2);

                    $d1 = strtotime($Date_1);
                    $d2 = strtotime($Date_2);
                    $days = (int)round(($d1-$d2 )/3600/24);
                    $realStage = substr($newWin['期号'],-3);
                    $allStage1 = (int)$days*$totalStage+$realStage;
                    $allStage2 = (int)substr($num->期号,-3);
                    $r[$i] =  $allStage1 - $allStage2;
                    if($days===0){
                        $r[$i] = $newWin['期号']-$num->期号;
                    }

                }
            }
        }
        return $r;

    }
    /*
     * 黑龙江快乐十分前三尾分布和遗漏
     */
    public function getFirstThreeWeiMissing($winNum){
        $re = array();
        $count = count($winNum);
        for($i=0;$i<$count;$i++){
            $winNum[$i]=array_slice($winNum[$i],0,3);
            foreach($winNum[$i] as $k=>$v){
                $winNum[$i][$k] = substr($v,-1);
            }
        }
        //前三尾分布
        $re['qiansanweifenbu'] = array_reverse($winNum);
        //missing
        for($j=0;$j<10;$j++){
            $m = 1;
            for($i=0;$i<$count;$i++){
                if(in_array($j,$winNum[$i])){
                    $re[$i][$j]=0;
                    $m = 0;
                }else{
                    $re[$i][$j]=$m;
                }
                $m++;
            }
        }

        return $re;
    }
    /*
     * 黑龙江快乐十分前三遗漏
     */
    public function getFirstThreeMissing($newWin,$stage){
        $newWin=array_slice($newWin,0,3);
        $count = 21;
        $r = array();
        for($i = 1;$i<$count;$i++){
            if(in_array($i,$newWin)){
                $r[$i]=0;
            }else{
                $s = $i;
                if($i<10){
                    $s = '0'.$i;
                }
                $num = DB::table('黑龙江快乐十分')->where('开奖号','like','%'.$s.'%')->where('期号','<',$stage)->select('期号','开奖号')->orderby('期号','desc')->first();
                $oldWin = array_slice(explode('-',$num->开奖号),0,3);
                while(!in_array($s,$oldWin)){
                    $num = DB::table('黑龙江快乐十分')->where('开奖号','like','%'.$s.'%')->where('期号','<',$num->期号)->select('期号','开奖号')->orderby('期号','desc')->first();
                    $oldWin = array_slice(explode('-',$num->开奖号),0,3);
                }
                $r[$i] = $stage-$num->期号;
            }
        }
        return $r;
    }
    /*
     * 黑龙江尾数分布
     */
    public  function getMantissa($WinNum,$stage){
        //尾数分布
        foreach($WinNum as $k=>$v){
            $WinNum[$k] = substr($v,-1);
        }
        $WinNum = array_unique($WinNum,SORT_NUMERIC);
        sort($WinNum);
        $t['weifenbu'] = $WinNum;
        //尾数遗漏
        $r = array();
        for($i = 0;$i<10;$i++){
            if(in_array($i,$WinNum)){
                $r[$i]=0;
            }else{
                $s = $i;
                $num = DB::table('黑龙江快乐十分')->where('期号','<',$stage)->where(function ($query) use ($s) {
                    $query->where('开奖号','like','%0'.$s.'%')->orwhere('开奖号','like','%1'.$s.'%')->orwhere('开奖号','like','%2'.$s.'%');
                })->select('期号')->orderby('期号','desc')->first();
                $r[$i] = $stage-$num->期号;
            }
        }
        $t['weiyilou'] = $r;
        return $t;
    }

    /*
     * 重号、前三重号
     */
    public function getRepeatNum($winNum,$type){
        $b_id = $winNum['ID']-1;
        $beforeWinNum = DB::table($type)->select('开奖号')->where('ID',$b_id)->first();
        $b_num = explode('-',$beforeWinNum->开奖号);

        if($type == '吉林快3'||$type=='吉林时时彩'){
            $b_num = str_split($beforeWinNum->开奖号);
        }
        $c=0;
        $d=0;
        $c_num = [$b_num[0],$b_num[1],$b_num[2]];
        for($i =0;$i<count($b_num);$i++){
            if(in_array($winNum['开奖号'][$i],$b_num)){
                $c +=1;
            }
        }
        for($i =0;$i<3;$i++){
            if(in_array($winNum['开奖号'][$i],$c_num)){
                $d +=1;
            }
        }

        $result = [$c,$d];
        return $result;
    }

    /*
     * 跨度、前三跨度、前三和值、和值、和值大小
     */
    public function span($winNum,$type){
        switch ($type){
            case '黑龙江11选5'://和值大小中间值为30
                $maxNum = 6;
                break;
            case '黑龙江快乐十分':
                $maxNum = 10;
                break;
            case '吉林快3'://和值大小  小于10为小 大于9为大
                $maxNum = 3;
                break;
            case '吉林时时彩':
                $maxNum = 4;
                break;
            case '四川金7乐':
                $maxNum = 6;
                break;
            case '四川快乐12':
                $maxNum = 6;
                break;

        }
        $winNumSumJiOu = '双';
        $winNumSum = array_sum($winNum);
        if($winNumSum%2 == 1){
            $winNumSumJiOu = '单';
        }
        $sumSize = '没有此功能';
        //处理和值大小
        if($type == '黑龙江11选5'){
            if($winNumSum > 30){
                $sumSize = '大';
            }else if($winNumSum < 30){
                $sumSize = '小';
            }else{
                $sumSize = '30';
            }
        }
        if($type == '吉林快3'){
            if($winNumSum > 9){
                $sumSize = '大';
            }
            if($winNumSum < 10){
                $sumSize = '小';
            }
        }

        $odd =0;  //奇数
        $bigNum=0;//大数
        for($m=0;$m<count($winNum);$m++){
            if($winNum[$m]%2==1){
                $odd = $odd+1;
            }
            if($winNum[$m]>$maxNum){
                $bigNum = $bigNum +1;
            }
        }

        $beforeNum = [$winNum[0],$winNum[1],$winNum[2]];
        $sum = $winNum[0]+$winNum[1]+$winNum[2];
        sort($winNum);
        sort($beforeNum);

        $span =end($winNum)-$winNum[0];
        $beforeSpan =end($beforeNum)-$beforeNum[0];

        $result = [$span,$beforeSpan,$sum,$winNumSum,$odd,$bigNum,$sumSize,$winNumSumJiOu];
        return $result;
    }

    /*
     * 前3和尾
     */
    public function firstSumLast($num){
        $sum = $num['0'] + $num['1'] +$num['2'];
        $sum = substr($sum,-1,1);
        return $sum;
    }

    /*
     * 吉林时时彩、前、中、后、和尾单双，和尾大小，后三位跨度，和尾
     */
    public function jiLinShiShiCai($data)
    {
        //dd($data['开奖号']);
        $isBigBefore = '小';
        $isBigMiddle = '小';
        $isBigAfter = '小';

        $isDoubleBefore = '双';
        $isDoubleMiddle = '双';
        $isDoubleAfter = '双';
        $winNumArray = $data['开奖号'];
        $b_sum = $winNumArray[0] + $winNumArray[1] + $winNumArray[2];
        $m_sum = $winNumArray[1] + $winNumArray[2] + $winNumArray[3];
        $a_sum = $winNumArray[2] + $winNumArray[3] + $winNumArray[4];
        $b_sum_f = substr($b_sum, -1);
        if ($b_sum_f > 4) {
            $isBigBefore = '大';
        }
        if ($b_sum_f % 2 == 1) {
            $isDoubleBefore = '单';
        }

        $m_sum_f = substr($m_sum, -1);
        if ($m_sum_f > 4) {
            $isBigMiddle = '大';
        }
        if ($m_sum_f % 2 == 1) {
            $isDoubleMiddle = '单';
        }

        $a_sum_f = substr($a_sum, -1);
        if ($a_sum_f > 4) {
            $isBigAfter = '大';
        }
        if ($a_sum_f % 2 == 1) {
            $isDoubleAfter = '单';
        }
        $newArray = [$winNumArray[2], $winNumArray[3], $winNumArray[4]];
        sort($newArray);
        $spanAfter = $newArray[2] - $newArray[0];


        $result = [$isDoubleBefore, $isDoubleMiddle, $isDoubleAfter, $isBigBefore, $isBigMiddle, $isBigAfter, $spanAfter, $b_sum_f, $m_sum_f, $a_sum_f];
        return $result;
    }

    /*
     * 获取连号
     */
    public function getContinuityNum($num,$max,$size){
        sort($num);
        $result = [];
        $lottery = new LotteryController();
        for($k=$size;$k>2;$k--){
            $allArr = $lottery->exhaustive($max,$k);
            $count = count($allArr);
            for($i=0;$i<$count;$i++){
                $allArr[$i] = explode(' ',$allArr[$i]);
                if($allArr[$i] == array_intersect($allArr[$i],$num)){
                    array_push($result,$allArr[$i]);
                }
            }
            if(isset($result)&&!empty($result)){
                break;
            }
        }

       return $result;
    }
    /*
     * 获取游戏信息
     */
    public  function  getGameInfo(Request $request){
        $type =  $request->get('type');
        switch($type) {
            case '1':
                $game = '黑龙江11选5';
                break;
            case '2':
                $game = '黑龙江快乐十分';
                break;
            case '3':
                $game = '吉林快3';
                break;
            case '4':
                $game = '吉林时时彩';
                break;
            case '5':
                $game = '四川金7乐';
                break;
            case '6':
                $game = '四川快乐12';
                break;
            default:
                return response()->json(['msg'=>'0','data'=>'参数错误']);
        }

        $gameData = DB::table('admin_game')->where('游戏中文名',$game)->select('代理Tel','代理QQ')->get();
        return response()->json(['msg'=>'1','data'=>$gameData]);
    }
    /*
     * 黑龙江快乐十分连号
     */
    public function lianHao($num){
        $re = $this->getContinuityNum($num,20,8);
        if($re && count($re[0])>3){
            //连号
            return count($re[0]);
        }else{
            //遗漏
            $re = DB::table('黑龙江快乐十分_任选四')->where('连号','1')->select('当前遗漏')->orderBy('当前遗漏','asc')->first();
            return $re->当前遗漏;
        }
    }

}

function obj2arr ($obj){
    return json_decode(json_encode($obj),true);
}